﻿
var baseUrl = 'http://' + document.location.host;

function LoadPage() {
    LoadQuestions();
}

function LoadQuestions() {
    var getQuestionsUrl = baseUrl + "/PollSystemService.svc/GetQuestions";
    $.ajax({
        type: "POST",
        async: true,
        url: getQuestionsUrl,
        data: null,
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            for (var i = 0; i < result.length; i++) {
                $('#mainbox').append('<div class="question" id=Question' + i + '>' + result[i] + '</div>');
                LoadAnswers(i);
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
}

function LoadAnswers(questionID) {
    var getAnswersUrl = baseUrl + "/PollSystemService.svc/GetAnswers";
    $.ajax({
        type: "POST",
        async: true,
        url: getAnswersUrl,
        data: JSON.stringify(questionID),
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            var question = $('#Question' + questionID);
            question.append('<ul>');
            for (var i = 0; i < result.length; i++) {
                question.append('<li><input type="radio" name="Question' + questionID + '" value="' + result[i] + '" />' + result[i] + '</li>');
            }
            question.append('</ul>');
            $('#Question' + questionID).append('<button class="votebutton" id="VoteButton' + questionID + '">Vote</button>');
            $('#VoteButton'+questionID).click(function () { clickVote(questionID) });
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
}

function clickVote(questionID) {
    var answer = $('input[name=Question' + questionID + ']:checked').val()
    var jData = { QuestionID: questionID, Answer: answer };
    var voteUrl = baseUrl + "/PollSystemService.svc/Vote";
    $.ajax({
        type: "POST",
        async: true,
        url: voteUrl,
        data: JSON.stringify(jData),
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            $('#welcomepage').hide();
            $('#resultpage').show();
            $('#backbutton').click(function () { getBack() });
            $('#result').append('<div id="voteresult"><p>' + result[0].Question + '</p></div>');
            var totalAnswers = 0;
            for (var i = 0; i < result.length; i++) {
                totalAnswers += result[i].AnswerCount;
            }
            for (var i = 0; i < result.length; i++) {
                var percentage = Math.round(result[i].AnswerCount / totalAnswers * 100);
                $('#voteresult').append('<div class="answer" style="float:left; clear:left">' + result[i].AnswerText + '</div>');
                $('#voteresult').append('<div class="greenbox"  style="float:left;width:' + percentage + '%">' + percentage + "%" + '</div>');
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });

    function getBack() {
        $('#voteresult').remove();
        $('#resultpage').hide();
        $('#welcomepage').show();
    }
}